package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskPhaseMapper;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskProjectAttachmentsMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskPhase;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskProjectAttachments;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskPhaseService;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskProjectAttachmentsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskProjectAttachmentsServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskProjectAttachmentsMapper, TaskAutotaskProjectAttachments>
        implements TaskAutotaskProjectAttachmentsService {
    @Override
    public void sync(ResultEntity content){
        log.debug("Auto task projectAttachments receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskProjectAttachments data = TaskAutotaskProjectAttachments.builder()
                .taskAutotaskProjectAttachmentsId(super.getLongValue(entity.get("id")))
                .title(ObjectUtils.isEmpty(entity.get("title"))?null : entity.get("title").asText())
                .attachedByContactId(ObjectUtils.isEmpty(entity.get("attachedByContactID"))?null : entity.get("attachedByContactID").asLong())
                .attachedByResourceId(ObjectUtils.isEmpty(entity.get("attachedByResourceID")) ? null: entity.get("attachedByResourceID").asLong())
                .attachDate(ObjectUtils.isEmpty(entity.get("attachDate"))||"null".equalsIgnoreCase(entity.get("attachDate").asText())?null :
                        LocalDateTime.parse(entity.get("attachDate").textValue(),DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .attachmentType(ObjectUtils.isEmpty(entity.get("attachmentType"))?null : entity.get("attachmentType").asText())
                .fileSize(ObjectUtils.isEmpty(entity.get("fileSize"))?null : entity.get("fileSize").asDouble())
                .contentType(ObjectUtils.isEmpty(entity.get("contentType"))?null : entity.get("contentType").asText())
                .fullPath(ObjectUtils.isEmpty(entity.get("fileSize"))?null : entity.get("fileSize").asText())
                .impersonatorCreatorResourceId(ObjectUtils.isEmpty(entity.get("impersonatorCreatorResourceID"))?null : entity.get("impersonatorCreatorResourceID").asInt())
                .opportunityId(ObjectUtils.isEmpty(entity.get("opportunityID"))? null : entity.get("opportunityID").asLong())
                .parentId(ObjectUtils.isEmpty(entity.get("parentID"))?null:entity.get("parentID").asLong())
                .publish(ObjectUtils.isEmpty(entity.get("publish"))?null :entity.get("publish").asInt())
                .data(ObjectUtils.isEmpty(entity.get("data"))?null :entity.get("data").asText())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskProjectAttachments dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskProjectAttachments>lambdaQuery()
                .eq(TaskAutotaskProjectAttachments::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskProjectAttachments::getTaskAutotaskProjectAttachmentsId, data.getImpersonatorCreatorResourceId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
